new Vue({
    el:"#app",
    data:{
        page: 1,  //显示的是哪一页
        pageSize: 10, //每一页显示的数据条数
        total: 0, //记录总数
        maxPage:6,
        allBrand:[],
        brand:{}, //当前添加或更新的对象
        title:"添加品牌",
        selectId:[],/*当前选择要删除的id*/
        searchEntity:{}/*搜索实体*/
    },
    methods:{

        pageHandler: function (page) {
            this.page = page;
            var _this = this;
            var url  = "/brand/getPageBrand.do?curPage="+this.page+"&pageSize="+this.pageSize;
            axios.post(url,_this.searchEntity).then(function (response) { //response服务器响应的数据
                var res = response.data;
                console.log(res)
                if (res.success == true){
                    _this.allBrand = res.data.dataList;
                    _this.total = res.data.total;
                }
            });
        },
        brandSave:function () {
            var _this = this;
            var url ='';
            // 区分是保存还是修改
            if(_this.brand.id != null){
                url = "/brand/update.do";
            }else{
                url = "/brand/add.do";
            }
            axios.post(url, _this.brand)
                .then(function (response) {
                    if ( response.data.success == true){
                        alert("ok");
                        this.pageHandler(1);/*跳转到第1页*/
                    }else {
                        alert("添加失败");
                    }
                    _this.brand={};
                });
        },
        updateClick:function (brand) {
            this.title="修改品牌";
            this.brand = JSON.parse(JSON.stringify(brand));
            console.log(this.brand);
        },
        deleteSelect:function (e,id) {
            /*判断当前是否为选中状态*/
            if (e.target.checked){
                /*保存到数组当中*/
                this.selectId.push(id);
            }else {
                /*从数组当中移除*/
                var idx = this.selectId.indexOf(id);
                this.selectId.splice(idx,1);
            }
        },
        deleteClick:function () {
            var _this = this;
            axios.post("/brand/deleteBrand.do",this.selectId).then(function (response) {
                if (response.data.success == true){
                    _this.pageHandler(1);
                } else {
                    alert("删除失败");
                }
            });
        }
    },
    created:function () {/*当vue创建完毕时,立即调用*/
        this.pageHandler(1);
    }

});